<template>
  <div>
    <div class="dialogPop">
      <el-dialog :visible.sync="addData.dialogAdd" :before-close="handleClose" size="tiny">
        <p class="dialog-title">{{addData.popTitle}}</p>
        <el-form :model="addData" :rules="rulesAdd"  ref="addData" label-width="100px">
                <el-row :gutter="20">
                    <el-col :span="23">
                    <el-form-item label="字典编号：">
                        <!-- <el-input v-model="addData.wordNum" :placeholder="addData.autoWarn" :disabled="addData.isDisabled"></el-input> -->
                        <span>自动生成</span>
                    </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="23">
                    <el-form-item label="字典名称：" prop="wordName">
                         <el-popover placement="top-start" trigger="focus" content="字符长度不大于20位">
                                <el-input v-model="addData.wordName" placeholder="请输入字典名称" :maxlength="20" slot="reference"></el-input>
                        </el-popover>
                    </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="23">
                    <el-form-item label="字典描述：" prop="wordDetail">
                        <el-popover placement="top-start" trigger="focus" content="字符长度不大于20位">
                         <el-input v-model="addData.wordDetail" placeholder="请输入字典描述" :maxlength="20" slot="reference"></el-input>
                        </el-popover>
                    </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="23">
                    <el-form-item label="字典类型：">
                        {{addData.wordType|filterType}}
                    </el-form-item>
                    </el-col>
                </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <p class="btn">
            <el-button @click="resetFormdelDialogAdd('addData')">取 消</el-button>
            <el-button type="primary" @click="addInfor('addData')" :loading="loadingBtn">确 认</el-button>
          </p>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { addDict, updateDict} from 'api/system';
export default {
    props: {
        addData: {
            default() {
                return {
                    dialogAdd: false // 默认不显示
                }
            },
            required: true
        }
    },
    filters: {
        filterType(val) {
            return ['', '科目字典'][val];
        }
    },
    data() {
        return {
            rulesAdd: {
                wordName: [
                    { required: true, message: '请输入字典名称', trigger: 'blur' },
                     { pattern: /^[^\s]+$/, message: '请勿输入空格', trigger: 'blur' }
                ],
                wordDetail: [
                     { required: true, message: '请输入字典描述', trigger: 'blur' },
                      { pattern: /^[^\s]+$/, message: '请勿输入空格', trigger: 'blur' }
                ]
            },
            loadingBtn: false
        }
    },
    methods: {
        // 点击弹窗的确认按钮，添加和修改信息
        addInfor(addData) {
            this.$refs[addData].validate((valid) => {
                if (valid) {// 验证通过
                    // 如果是添加弹窗
                    this.loadingBtn=true;
                    let desc=this.addData.wordDetail;
                    let name=this.addData.wordName;
                    if (this.addData.status==1) {
                        addDict(desc, name).then(res => {
                            this.loadingBtn=false;
                            // this.$emit('searchList');
                            this.$emit('searchList', 'search');
                            this.$message({
                                type: 'success',
                                message: '添加成功!'
                            });
                            this.handleClose();
                        }).catch(() => {
                            this.loadingBtn=false;
                        });
                    }
                    // 如果是修改弹窗
                    if (this.addData.status==2) {
                        let id=this.addData.wordNum;
                        updateDict(desc, name, id).then(res => {
                            this.loadingBtn=false;
                            // this.$emit('searchList');
                            this.$emit('searchList', 'caches');
                            this.$message({
                                type: 'success',
                                message: '修改成功!'
                            });
                            this.handleClose();
                        }).catch(() => {
                            this.loadingBtn=false;
                        });
                    }
                } 
            });
        },
       // 弹出框的关闭按钮
        handleClose(done) {
            this.$refs.addData.resetFields();
            this.delDialogAdd();
        },
        resetFormdelDialogAdd(addData) {
            this.$refs[addData].resetFields();
            this.delDialogAdd();
        },
        // 还原弹窗信息（addDate是接受的父组件，不可以addDate={}这样修改）
        delDialogAdd() {
            this.addData.dialogAdd=false;
            this.addData.wordNum='';
            this.addData.wordName='';
            this.addData.wordDetail='';
        }
    }
}
</script>
<style lang="scss" scoped>
@import '../../style/dialog.scss';
</style>

